package com.thb.pay.manage.web.agent;

import com.thb.pay.api.agent.AgentAccountServiceApi;
import com.thb.pay.api.agent.vo.resp.AgentAccountVo;
import com.thb.pay.api.sys.SysConfigServiceApi;
import com.thb.pay.api.sys.vo.resp.SysConfigVo;
import com.thb.pay.core.annotation.OperationLog;
import com.thb.pay.core.enums.CommConfigEnum;
import com.thb.pay.core.enums.ExceptionEnum;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.R;
import com.thb.pay.manage.comm.controller.BaseController;
import com.thb.pay.vo.req.DecreaseAgentAmountReq;
import com.thb.pay.vo.req.IncreaseAgentAmountReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @类名称 AgentAccountController
 * @类描述 <pre>代理商账户信息控制类</pre>
 * @作者 lucky
 * @创建时间 2019/10/12
 * @版本 v1.01
 * </pre>
 */
@Slf4j
@RestController
@RequestMapping("api/sys/agentAccount")
public class AgentAccountController extends BaseController {

    @Autowired
    private AgentAccountServiceApi agentAccountServiceApi;


    @Autowired
    private SysConfigServiceApi sysConfigServiceApi;

    @GetMapping("/get")
    @ResponseBody
    public R get(@RequestParam("agentId") Long agentId) {
        AgentAccountVo resultData = agentAccountServiceApi.getById(agentId);
        return R.ok(resultData);
    }

    @PostMapping("/increaseCountAmount")
    @OperationLog(value = "代理商资金增加")
    public R increaseCountAmount(@RequestBody IncreaseAgentAmountReq increaseAmount) {
        // TODO 具体的钱类型 目前将元 (可含小数点两位)
        Long amount = new BigDecimal(increaseAmount.getAmount().trim()).multiply(new BigDecimal(100)).longValue();
        String adminAuthPassword = CommConfigEnum.ADMIN_AUTH_PASSWORD.code();
        SysConfigVo sysConfigVo = sysConfigServiceApi.getByCode(CommConfigEnum.ADMIN_AUTH_PASSWORD.code());
        if (sysConfigVo != null) {
            adminAuthPassword = sysConfigVo.getValue();
        }
        if (!adminAuthPassword.equals(increaseAmount.getAuthPassword())) {
            throw RRException.build(ExceptionEnum.AUTH_ADMIN_PASSWORD_NOT_MATCH);
        }
        agentAccountServiceApi.increaseAmountToAccount(increaseAmount.getBelongPlatUserId(), amount, increaseAmount.getBizType(), increaseAmount.getBizItem(), null);
        return R.ok();
    }

    @RequestMapping("/decreaseAccountAmount")
    @OperationLog(value = "代理商资金减少")
    public R decreaseAccountAmount(@RequestBody DecreaseAgentAmountReq decreaseAmount) {
        // TODO 具体的钱类型 目前将元 (可含小数点两位)
        Long amount = new BigDecimal(decreaseAmount.getAmount().trim()).multiply(new BigDecimal(100)).longValue();
        String adminAuthPassword = CommConfigEnum.ADMIN_AUTH_PASSWORD.code();
        SysConfigVo sysConfigVo = sysConfigServiceApi.getByCode(CommConfigEnum.ADMIN_AUTH_PASSWORD.code());
        if (sysConfigVo != null) {
            adminAuthPassword = sysConfigVo.getValue();
        }
        if (!adminAuthPassword.equals(decreaseAmount.getAuthPassword())) {
            throw RRException.build(ExceptionEnum.AUTH_ADMIN_PASSWORD_NOT_MATCH);
        }
        agentAccountServiceApi.decreaseAmountToAccount(decreaseAmount.getBelongPlatUserId(), amount, decreaseAmount.getBizType(), decreaseAmount.getBizItem());
        return R.ok();
    }


}
